package com.lam.system.controller;

import java.util.List;
import java.util.stream.Collectors;

import com.lam.common.base.controller.BaseController;
import com.lam.common.base.domain.Result;
import com.lam.common.validate.group.Add;
import com.lam.common.validate.group.Select;
import com.lam.framework.annotation.ParamInject;
import com.lam.framework.operLog.annotation.Log;
import com.lam.framework.operLog.enums.BusinessType;
import com.lam.system.entity.SysRoleMenu;
import com.lam.system.param.SysRoleMenuParam;
import com.lam.system.service.SysRoleMenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 角色菜单Controller
 * 
 * @author admin
 * @date 2022-09-01
 */
@Log(title = "角色菜单")
@ParamInject
@RestController
@RequestMapping("/${adminPath}/sys/roleMenu")
public class SysRoleMenuController extends BaseController {

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    /**
     * 查询角色菜单列表
     */
    @Log(oper="角色授权的菜单查询")
    @PreAuthorize("@ss.hasPm('sys:roleMenu:list')")
    @GetMapping("/roleMenulist")
    public Result roleMenulistAdm(@Validated(Select.class) SysRoleMenuParam param) {
    
        List<SysRoleMenu> list = sysRoleMenuService.selectList(param);
        
        return Result.ok(list.stream().map(t->t.getMenuId()).collect(Collectors.toList()));
    }
    
    /**
     * 新增角色菜单
     */
    @PreAuthorize("@ss.hasPm('sys:roleMenu:add')")
    @Log(businessType = BusinessType.INSERT, oper = "保存角色菜单权限")
    @PostMapping
    public Result add(@Validated(Add.class) @RequestBody SysRoleMenuParam param) {
    
    	sysRoleMenuService.insert(param);
        return Result.ok();
    }

}
